﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.News
{
    public class NewsCategoryBL
    {
        public void clsNewcategoryInsert(tbl_NewsCategory newscategorys)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_NewsCategory newcategory = new tbl_NewsCategory();
                    newcategory.CategoryName = newscategorys.CategoryName;
                    newcategory.Link = newscategorys.Link;
                    newcategory.Description = newscategorys.Description;
                    newcategory.CreatedDate = newscategorys.CreatedDate == null ? DateTime.Now : newscategorys.CreatedDate;
                    newcategory.CreatedUser = newscategorys.CreatedUser;
                    newcategory.Status = newscategorys.Status;

                    adlerContextDb.tbl_NewsCategories.InsertOnSubmit(newcategory);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public void clsNewcategoryModify(tbl_NewsCategory newscategorys)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var newcategory = adlerContextDb.tbl_NewsCategories.Single(d => d.CategoryNo == newscategorys.CategoryNo);
                    newcategory.CategoryName = newscategorys.CategoryName;
                    newcategory.Link = newscategorys.Link;
                    newcategory.Description = newscategorys.Description;
                    newcategory.Status = newscategorys.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public void clsNewcategoryDelete(tbl_NewsCategory newscategorys)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var newcategory = adlerContextDb.tbl_NewsCategories.Single(d => d.CategoryNo == newscategorys.CategoryNo);
                    newcategory.Status = newscategorys.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        #region backup
        public List<tbl_NewsCategory> GetNewCategories()
        {
            AdlerContextDataContext adlerContextDb = new AdlerContextDataContext();
            var newsCategories = adlerContextDb.tbl_NewsCategories.ToList();
            return newsCategories;
        }

        public tbl_NewsCategory GetNewsCategoryById(int id)
        {
            AdlerContextDataContext adlerContextDb = new AdlerContextDataContext();
            var newsCategory = adlerContextDb.tbl_NewsCategories.FirstOrDefault<tbl_NewsCategory>(x => x.CategoryNo == id);
            return newsCategory;
        }

        // Show news category.
        public List<MODEL_SP_ADMIN_SEARCH_NEWSCATEGORY> Get_Admin_NewsCategory(string categoryname, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_NEWSCATEGORY(categoryname, status)
                //            select new MODEL_SP_ADMIN_SEARCH_NEWSCATEGORY
                //            {
                //                Index = index++,
                //                CategoryNo = n.CategoryNo,
                //                CategoryName = n.CategoryName,
                //                Link = n.Link,
                //                Description = n.Description,
                //                CreatedDate = n.CreatedDate,
                //                CreatedUser = n.CreatedUser,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_NewsCategory GetNewsCategoryByCategoryno(int categoryno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_NewsCategories.Single(d => d.CategoryNo == categoryno);
            }
        }
#endregion

        public IEnumerable<MODEL_SP_ADMIN_SEARCH_NEWSCATEGORY> GetNewsCategoryByLangId(int langId) 
        {
            var dbcontext = new AdlerContextDataContext();
           var re = dbcontext.tbl_NewsCategories.Select(x => new MODEL_SP_ADMIN_SEARCH_NEWSCATEGORY()
                                                                  {
                                                                      StoreLanguage =
                                                                          GetStoreLangugageAttributes(langId,
                                                                                                      x.CategoryNo,
                                                                                                      dbcontext),
                                                                    CategoryName = x.CategoryName,
                                                                    CategoryNo = x.CategoryNo,
                                                                    Description = x.Description,
                                                                  });
            

            return re;
        }

        public List<tbl_StoreLanguageAtrribute> GetStoreLangugageAttributes(int langId, int entityId, AdlerContextDataContext dbcontext)
        {
            if (langId == 0)
            {
                langId = 2;
            }

            var storeLang = dbcontext.tbl_StoreLanguageAtrributes
                .Where(x => x.LangId == langId && x.IsDeleted == false && x.EntityId == entityId);
            return storeLang.ToList();
        }

        public List<tbl_StoreLanguageAtrribute> GetStoreLangugageAttributes(int langId, int entityId)
        {
            var dbcontext = new AdlerContextDataContext();
            if(langId==0)
            {
                langId = 2;
            }

            var storeLang = dbcontext.tbl_StoreLanguageAtrributes
                .Where(x => x.LangId == langId && x.IsDeleted == false && x.EntityId == entityId);
            return storeLang.ToList();
        }

        public void SaveStoreEnglish(tbl_StoreLanguageAtrribute model,bool addnew)
        {
            using (var adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    if(addnew)
                    {
                        adlerContextDb.tbl_StoreLanguageAtrributes.InsertOnSubmit(model);
                        adlerContextDb.SubmitChanges();
                    }
                    else
                    {
                        var updateModel = adlerContextDb.tbl_StoreLanguageAtrributes.Single(x => x.Id == model.Id);
                        updateModel.Attrribute = model.Attrribute;
                        updateModel.ValueAttr = model.ValueAttr;

                        adlerContextDb.SubmitChanges();
                    }
                    
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public tbl_StoreLanguageAtrribute GetStoreEnglishById(int id)
        {
            var adlerContextDb = new AdlerContextDataContext();
            return adlerContextDb.tbl_StoreLanguageAtrributes.FirstOrDefault(x => x.Id == id);
        }
    }
}
